package cn.doitedu.sql;

import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class _10_WindowGroupTopn {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.enableCheckpointing(1000, CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setCheckpointStorage("file:///d:/ckpt");

        env.setParallelism(1);


        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
        tenv.getConfig().set("table.exec.source.idle-timeout","1s");


        // 建表，映射kafka中的用户行为日志topic
        tenv.executeSql(
                " create table action_log(                                      "+
                        "   uid int,                                            "+
                        " 	event_id string,                                    "+
                        " 	properties map<string,string>,                      "+
                        " 	action_time bigint,                                 "+
                        " 	pt as proctime(),                                   "+
                        " 	rt as to_timestamp_ltz(action_time,3),              "+
                        " 	watermark for rt as rt                              "+
                        "  ) with (                                             "+
                        "    'connector' = 'kafka',                             "+
                        "    'topic' = 'action-log',                            "+
                        "    'properties.bootstrap.servers' = 'doitedu:9092',   "+
                        "    'properties.group.id' = 'g003',                    "+
                        "    'scan.startup.mode' = 'latest-offset',             "+
                        "    'value.format' = 'json',                           "+
                        "    'value.fields-include' = 'EXCEPT_KEY'              "+
                        " )                                                     "
        );


        tenv.executeSql(
                " WITH tmp AS (                                                                                    "+
                        " SELECT                                                                                           "+
                        "     window_start,                                                                                "+
                        "     window_end,                                                                                  "+
                        "     properties['url'] as url,                                                                    "+
                        "     uid,                                                                                         "+
                        "     count(event_id) FILTER(where event_id = 'page_load') as cnt                                  "+
                        " FROM TABLE(                                                                                      "+
                        "     TUMBLE(TABLE action_log,DESCRIPTOR(rt),INTERVAL '5' MINUTE)                                  "+
                        " )                                                                                                "+
                        " GROUP BY                                                                                         "+
                        "     window_start,                                                                                "+
                        "     window_end,                                                                                  "+
                        "     properties['url'],                                                                           "+
                        "     uid                                                                                          "+
                        " )                                                                                                "+
                        "                                                                                                  "+
                        " SELECT                                                                                           "+
                        "     window_start,                                                                                "+
                        "     window_end,                                                                                  "+
                        "     url,                                                                                         "+
                        "     uid,                                                                                         "+
                        "     cnt,                                                                                         "+
                        "     rn                                                                                           "+
                        " FROM (                                                                                           "+
                        "     SELECT                                                                                       "+
                        "         window_start,                                                                            "+
                        "         window_end,                                                                              "+
                        "         url,                                                                                     "+
                        "         uid,                                                                                     "+
                        "         cnt,                                                                                     "+
                        "         row_number() over(partition by window_start,window_end,url order by cnt desc ) as rn     "+
                        "     FROM tmp  ) tmp2                                                                             "+
                        " WHERE rn<=2                                                                                      "
        ).print();




    }
}
